<?php

namespace App\Services\Setting;

use App\Services\Setting\Setting\SettingLogic;
use App\Services\Manage\Api;

class Setting
{

    //添加配置或编辑某个配置时 进行数据检验 分类的存在性检验
    protected function _checkCateData($data, bool $isEdit): array
    {
        $validateInitData = $isEdit ? ['requiredId' => true] : [];
        ValidateSetting::checkValidate($data, ['id', 'category_id', 'name', 'value', 'action', 'type', 'order'], true, $validateInitData);

        if($isEdit){  //编辑
            $setInfo = (new SettingLogic())->getOneSet(['id'=>$data['id']]);
            if($setInfo['status'] != SUCCESS_STATUS){
                return verify_err(null, '该分类不存在');
            }

            $setType = $data['type'] ?? $setInfo['res']['type'];
            $setValue = $data['value'] ?? $setInfo['res']['value'];
            $setOtherInfo = $data['other_info'] ?? $setInfo['res']['other_info'];
            if(in_array($setType, [1,2]) && !empty(array_diff(explode(",", $setValue), array_flip((array)$setOtherInfo)))){
                return verify_err(null, '当类型为radio或checkbox时，value必须在other_info数组中');
            }
            if(isset($data['action'])){
                if($setInfo['res']['action'] == $data['action']){ return auto_success($data, '可操作'); }
            }
        }else{   //添加
            $other_info = json_decode($data['other_info'],true);
            if(in_array($data['type'], [1,2]) && !empty(array_diff(explode(",", $data['value']), array_flip($other_info)))){
                return verify_err(null, '当类型为radio或checkbox时，value必须在other_info数组中');
            }
        }

        if(isset($data['action'])){
            $checkAction = (new SettingLogic())->checkAction($data);
            if($checkAction['status'] != SUCCESS_STATUS){
                return $checkAction;
            }
        }

        return auto_success($data, '可操作');
    }
//
//    /*添加一个配置*/
//    public function addSet(): array
//    {
//        $data = $this->receiveData(['app_id'=>0,'category_id'=>0,'name'=>' ','value'=>'','action'=>' ','type'=>0,'order'=>100,'other_info'=>[]]);
//
//        $checkData = $this->_checkCateData($data, false);
//        if($checkData['status'] != SUCCESS_STATUS){
//            return $checkData;
//        }
//        return $this->logic($this->settingLogic)->addSet($data);
//    }
//
    /**
     * 编辑一个配置
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function editSet($data): array
    {

        $initMap = ['app_id' => 0, 'id' => 0, 'category_id' => 0, 'name' => ' ', 'value' => ' ', 'action' => ' ', 'type' => 0, 'order' => 100, 'other_info' => []];
        $data = Api::addAppId($data);
        $data = init_array($data, $initMap);
        ValidateSetting::checkValidate($data, array_keys($initMap));

        if(is_string($data['other_info'])){
            $data['other_info'] = json_decode($data['other_info'], true);
        }

        $checkData = $this->_checkCateData($data, true);
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }

        return (new SettingLogic())->editSet($data);
    }


    /**
     * 获取某个具体配置
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function getOneSet($data): array
    {
        $data = init_array($data, ['id' => 0, 'action' => '']);
        ValidateSetting::checkValidate($data, ['id', 'action']);

        if(!isset($data['id']) && !isset($data['action'])){
            return verify_err(null, 'id与action两个值必须传一个');
        }

        return (new SettingLogic())->getOneSet($data);
    }

//    /*根据action获取一个或多个配置详情*/
//    public function getSomeSet(): array
//    {
//        $data = $this->issetReceiveData(['action'=>' ','app_id'=>0]);
//        return $this->stepRun($data, 'getSomeSet');
//    }
//
    /**
     * 获取某应用配置列表
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function getAppSet($data): array
    {
        $data = init_array($data, ['page' => 1, 'listRows' => 15]);
        ValidateSetting::checkValidate($data, ['page', 'listRows']);

        return (new SettingLogic())->getAppSet($data);
    }
//
//    /*获取某些分类下的配置列表*/
//    public function getCateSetList(): array
//    {
//        $data = $this->haveIssetReceiveData(['action'=>[], 'app_id'=>0]);
//        return $this->stepRun($data, 'getCateSetList', 'getCateSetList');
//    }
    /**
     * 添加具体配置
     * @param $data
     * @return array
     */
    public function addSet($data): array
    {
        $data = init_array($data,['app_id'=>0,'category_id'=>0,'name'=>' ','value'=>'','action'=>' ','type'=>0,'order'=>100,'other_info'=>[]]);
        ValidateSetting::checkValidate($data, ['category_id','name','value','action','type','order','other_info']);

        $checkData = $this->_checkCateData($data, false);
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }
        return (new SettingLogic())->addSet($data);
    }

}